<?php

namespace App\Http\Controllers;

use App\Http\Requests\LoginRequest;
use App\Models\System\Admin;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Mews\Captcha\Facades\Captcha;

class LoginController extends Controller
{
    public function login(LoginRequest $request): JsonResponse
    {
        $admin = $request->validateUser();
        $token = $admin->createToken('test');

        return $this->success([
            'accessToken' => $token->plainTextToken,
        ], '登录成功');
    }

    public function logout(Request $request): JsonResponse
    {
        /** @var $admin Admin */
        $admin = $request->user();

        $admin->tokens()->where('id', $admin->currentAccessToken()->id)->delete();

        return $this->success([], '退出成功');
    }

    public function captcha(): JsonResponse
    {
        $img = Captcha::create('mini', true);

        return $this->success([
            'captchakey' => $img['key'],
            'captchaimg' => $img['img'],
        ]);
    }
}
